import { Card } from "./Card";
import SGS_Util from "../core/utils";

export class Store {
	public cards: Card[] = [];
	public trash: Card[] = [];

	constructor(cards?: Card[]) {
		if (cards) {
			this.cards = cards;
		}
	}

	dispatch(cardCount?: number): Card[] {
		if (cardCount === undefined || cardCount <= 0) {
			this.cards = SGS_Util.shuffle(this.cards);
			return [];
		} else if (this.cards.length <= cardCount) {
			const copyCards = this.cards.slice();
			this.cards = SGS_Util.shuffle(this.trash);
			this.trash = [];
			return this.cards.length === cardCount
				? [...copyCards]
				: [...copyCards, ...this.dispatch(cardCount - this.cards.length)];
		} else {
			return this.cards.splice(0, cardCount);
		}
	}

	throwCards(cards: Card[]) {
		this.trash.push(...cards);
	}

	countCards() {
		return this.cards.length;
	}

	countTrash() {
		return this.trash.length;
	}
}
